#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::H3C::DisplayPower < Aio::Module::Cmd::H3C
  include Aio::Module

  def initialize
    super({
      :cmd_full 		=> "display power",
      :cmd_short 		=> "dis pow",
      :author				=> "Elin",
      :description  => "This is H3C Command# display power",
      :ranking			=> Ranking_1,
      :platform			=> "all",
      :benchmark		=> {
        :power_status	=> ["=~", /Normal/, /Absent/],
      },
    })
  end

  def parse
    cont = self.context.dup
    useful[:power] = {}
    case device_template
    when Template_1
      cont.readline_range_loop(/Slot/, /Slot/) do |cont_layer|
        parse_slot(cont_layer)
      end
    when Template_2
      cont.readline_range_loop(/Unit/, /Unit/) do |cont_layer|
        parse_unit(cont_layer)
      end
    end
  end

=begin
  def parse_def(context)
    case context[0]
    when /Slot/
      parse_slot(context)
    when /Unit/
      parse_unit(context)
    end
  end
=end

  def parse_slot(context)
    power = {}
    context.readline_match_block(/Slot (?<slot_id>\d+)/) do |block|
      useful[:power] = {}
      useful[:power]["slot_#{block[:slot_id]}"] = power
      block.update(power, :slot_id)
    end
    context.readline_range_loop(/Power/, reg_blank) do |cont_layer|
      parse_power(cont_layer, power)
    end
  end

  def parse_unit(context)
    power = {}
    context.readline_match_block(/Unit (?<slot_id>\d+)/) do |block|
      useful[:power] = {}
      useful[:power]["slot_#{block[:slot_id]}"] = power
      block.update(power, :slot_id)
    end
    context.readline_range_loop(/power/, /power/, {:before=>true}) do |cont_layer|
      parse_power(cont_layer, power)
    end
  end

  def parse_power(context, power)
    power_id_info = {}
    context.readline_match_block(/[Pp]ower (?<power_id>\d+)/) do |block|
      power_id = block[:power_id]
      power["power_#{power_id}"] = power_id_info
      block.update(power_id_info, :power_id)
    end
    context.readline_match_block(/State : (?<power_status>.*)/) do |block|
      block.warning_serious(power_id_info, :power_status, self)
    end

    context.readline_match_block(/Type : (?<type>.*)/) do |block|
      block.update(power_id_info, :type)
    end
  end
end



